package com.shjy.driver.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class HttpFileUtil {
	private static final String Curdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());// 时间

	public static void main(String[] args) {
//		HttpFileUtil.sendFormFile("http://139.224.80.49/shjy/driver/uploadVehicle.htm", new HashMap<String, String>(),
//				"C:\\Users\\wang\\Desktop\\kj\\sikuli\\login.png");
	}

	private static final String BOUNDARYSTR = "XMKSSS***********";
	private static final String BOUNDARY = "--" + BOUNDARYSTR + "\r\n";

	// 文件上传
	public static String sendFormFile(String urlString, Map<String, String> propertys, File file, String fileElementIdfull) {
		HttpURLConnection urlConnection = null;
		FileInputStream fi = null;
		OutputStream o = null;
		String result = "";
		try {
			URL url;
			url = new URL(urlString);
			urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.setRequestMethod("POST");

			urlConnection.setDoOutput(true);
			urlConnection.setDoInput(true);
			urlConnection.setUseCaches(false);

			urlConnection.setRequestProperty("Content-type", "multipart/form-data;boundary=" + BOUNDARYSTR);
			if (propertys != null)
				for (String key : propertys.keySet()) {
					urlConnection.addRequestProperty(key, propertys.get(key));
				}
			urlConnection.connect();
			BufferedOutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());

			// 表单数据
			Map<String, String> parameters = new HashMap<String, String>();
//			parameters.put("guid", "61080c526d8d48099f61744f9cf89993");
//			parameters.put("billId", "17");
//			parameters.put("pictureId", "");
//			parameters.put("fileElementId", "idCardImage");
//			parameters.put("driverUuid", "46dafe880cbc49b69078de5acc7d3e41");
			
			if (parameters != null) {
				StringBuffer param = new StringBuffer();
				for (String key : parameters.keySet()) {
					param.append(BOUNDARY);
					param.append("Content-Disposition:form-data;name=\"");
					param.append(key);
					param.append("\"\r\n\r\n");
					param.append(parameters.get(key));
					param.append("\r\n");
				}
				out.write(param.toString().getBytes());
			}
			// 文件字节数据
			StringBuilder filenamesb = new StringBuilder();
			filenamesb.append(
					"Content-Disposition:form-data;Content-Type:application/octet-stream;name=\""+fileElementIdfull+"\";filename=\"");// image/png
			filenamesb.append(file.getName() + "\"\r\n\r\n");
			out.write(BOUNDARY.getBytes());
			out.write(filenamesb.toString().getBytes());
			FileInputStream fis = new FileInputStream(file);
			byte[] buffer = new byte[81920]; // 8k
			int count = 0;
			// 读取文件
			while ((count = fis.read(buffer)) != -1) {
				out.write(buffer, 0, count);
			}
			out.write("\r\n\r\n".getBytes());
			fis.close();
			out.write(("--" + BOUNDARYSTR + "--\r\n").getBytes());
			out.flush();
			out.close();

			// 定义BufferedReader输入流来读取URL的响应
			BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
			String line = "";
			while ((line = in.readLine()) != null) {
				result += line;
			}
			System.out.println(urlString + "====" + result);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fi != null) {
					fi.close();
				}
				if (o != null) {
					o.close();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (urlConnection != null) {
				urlConnection.disconnect();
			}
		}
		return result;
	}
}
